﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GovernmentOfficeProject.LevelDataAccessModel;
using GovernmentOfficeProject.LevelBusinessLogic;
using GovernmentOfficeProject.SysTools;

namespace GovernmentOfficeProject.LevelViewUI.information
{
    public partial class InformSms : System.Web.UI.Page
    {
        decimal dclLoginUserID = -1;
        
        private static List<Entity_GMPersonnelInfo> listSendPersonnelToGV = new List<Entity_GMPersonnelInfo>();    //保存待发送人员列表.
        private static List<Entity_GMPersonnelInfo> listPersonnelToGV = new List<Entity_GMPersonnelInfo>();            //保存个人通讯簿人员列表.
        private static List<Entity_GMPersonnelInfo> listSysToGV = new List<Entity_GMPersonnelInfo>();                     //保存系统通讯簿人员列表.

        BLL_USER bllUser = new BLL_USER();                                               //用于获取所有系统用户信息.
        BLL_Contacts bllContacts = new BLL_Contacts();                                //用于获取所有个人通讯簿(联系人)信息.    
        BLL_SmsOperation bllSmsOperation = new BLL_SmsOperation();       //用于发送短信操作.
        BLL_Department bllDept = new BLL_Department();                            //用于获取部门信息.

        List<Entity_USER_Contacts> listPersonalContacts = null;            //个人通讯簿中的人员信息.
        List<Entity_USER_User> listSysUser = null;                          //系统中的用户信息.
        
        protected void Page_Load(object sender, EventArgs e)
        {
            //权限判断
            if (SystemContext.CurrentUserID == 0)
            {
                Response.Redirect("~/Login.htm?FromUrl=" + HttpUtility.UrlEncode(Request.Url.AbsoluteUri));
            }
            else
            {
                SystemContext.CurrentUserID = SystemContext.CurrentUserID;

            }
            dclLoginUserID = SysTools.SystemContext.CurrentUserID;
            if (!Page.IsPostBack)
            {
                listSendPersonnelToGV = new List<Entity_GMPersonnelInfo>();
                listPersonnelToGV = new List<Entity_GMPersonnelInfo>();
                listSysToGV = new List<Entity_GMPersonnelInfo>();
                gridDataBindByGridViewPersonal();
                gridDataBindByGridViewSys();
                ddlSysDeptValuesBind();
                gridDataBindByGridViewInformPersonnel();  
            }
        }


        /***************************选择联系人操作-个人通讯簿*********************************/
        //一、获取并绑定所有个人通讯簿人员列表：
        //1.1、为GridViewPersonal绑定所有用户组.
        private bool gridDataBindByGridViewPersonal()
        {
            try
            {
                bool flag = false;
                if (listPersonnelToGV.Count > 0)
                {
                    gridDataBindByGridViewPersonal(listPersonnelToGV);
                }
                else
                {
                    Entity_USER_Contacts personalContactsEntity = new Entity_USER_Contacts();
                    personalContactsEntity.UserId = dclLoginUserID;
                    listPersonalContacts = bllContacts.getContactsList(personalContactsEntity);
                    Entity_GMPersonnelInfo gmPerInfo = null;
                    foreach (Entity_USER_Contacts personal in listPersonalContacts)
                    {
                        gmPerInfo = new Entity_GMPersonnelInfo();
                        gmPerInfo.SmsInfoName = personal.Name;
                        gmPerInfo.SmsInfoMobileNumber = personal.MobileNumber;
                        gmPerInfo.SmsInfoType = "PERSONAL";
                        if (personal.Company.Length > 10)
                        {
                            gmPerInfo.SmsInfoDeptName = personal.Company.Substring(0, 10) + "...";
                        }
                        else
                        {
                            gmPerInfo.SmsInfoDeptName = personal.Company;
                        }
                        listPersonnelToGV.Add(gmPerInfo);
                    }
                    if (listPersonnelToGV.Count != 0)
                    {
                        gridDataBindByGridViewPersonal(listPersonnelToGV);
                    }
                }
                return flag;
            }
            catch (System.Exception ex)
            {
                return false;
                throw ex;
            }
        } 
        //1.2、根据传入list为GridViewPersonal绑定数据.
        private bool gridDataBindByGridViewPersonal(object objDataSource)
        {
            try
            {
                this.divGVPersonal.InnerText = "";
                this.GridViewPersonal.DataSource = (List<Entity_GMPersonnelInfo>)objDataSource;
                this.GridViewPersonal.DataBind();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
                throw ex;
            }
        }
        //二、基本功能操作：
        //2.1、实现根据姓名查找功能.
        protected void lBtnPersonalFind_Click(object sender, EventArgs e)
        {
            this.divGVPersonal.InnerText = "";
            string strPersonalName = this.txtPersonalFindName.Text.Trim();
            if (strPersonalName == "")
            {
                gridDataBindByGridViewPersonal(null);
                this.divGVPersonal.InnerText = "请输入名字查找,或点击全部按钮获取所有联系人.";
            }
            else
            {
                Entity_USER_Contacts personalContactsParameter = new Entity_USER_Contacts();
                personalContactsParameter.Name = strPersonalName;
                personalContactsParameter.UserId = dclLoginUserID;
                List<Entity_USER_Contacts> perConList = bllContacts.getContactsList(personalContactsParameter);
                List<Entity_GMPersonnelInfo> gmPerInfoList = new List<Entity_GMPersonnelInfo>();
                Entity_GMPersonnelInfo gmPerInfo = null;
                if (perConList.Count != 0)
                {
                    foreach (Entity_USER_Contacts perCon in perConList)
                    {
                        gmPerInfo = new Entity_GMPersonnelInfo();
                        gmPerInfo.SmsInfoName = perCon.Name;
                        gmPerInfo.SmsInfoDeptName = perCon.Company;
                        gmPerInfo.SmsInfoMobileNumber = perCon.MobileNumber;
                        gmPerInfo.SmsInfoType = "PERSONAL";
                        foreach (Entity_GMPersonnelInfo gmp in listSendPersonnelToGV)
                        {
                            if (gmp.SmsInfoName == perCon.Name && gmp.SmsInfoMobileNumber == perCon.MobileNumber && gmp.SmsInfoType == "PERSONAL")
                            {
                                gmPerInfo.SmsInfoSelected = gmp.SmsInfoSelected;
                            }
                        }
                        gmPerInfoList.Add(gmPerInfo);
                    }
                    this.divGVPersonal.InnerText = "";
                    gridDataBindByGridViewPersonal(gmPerInfoList);
                }
                else
                {
                    gridDataBindByGridViewPersonal(null);
                    this.divGVPersonal.InnerText = "没有联系人[" + strPersonalName + ",]请检查查询姓名是否输入正确.";
                }
            }
        }
        //2.2、实现查找全部按钮.
        protected void lBtnPersonalALL_Click(object sender, EventArgs e)
        {
            gridDataBindByGridViewPersonal();
        }
        //三、操作GridViewInformPersonnel的绑定：
        //3.1选择或取消时:操作GridViewInformPersonnel的绑定.
        protected void CheckBoxSelectedPersonal_CheckedChanged(object sender, EventArgs e)
        {
            GridViewRow gdr = (GridViewRow)((CheckBox)sender).Parent.Parent;

            string strName = "";
            if (gdr.Cells[0].Text.Trim().Equals("&nbsp") || gdr.Cells[0].Text.Trim().Equals(""))
            { strName = ""; }
            else
            { strName = gdr.Cells[0].Text.Trim(); }
            string strDeptName = "";
            if (gdr.Cells[1].Text.Trim().Equals("&nbsp") || gdr.Cells[1].Text.Trim().Equals(""))
            { strDeptName = ""; }
            else
            { strDeptName = gdr.Cells[1].Text.Trim(); }
            string strMobileNo = "";
            if (gdr.Cells[2].Text.Trim().Equals("&nbsp") || gdr.Cells[2].Text.Trim().Equals(""))
            {
                strMobileNo = "";
            }
            else
            {
                strMobileNo = gdr.Cells[2].Text.Trim();
            }

            if (strMobileNo.Length != 11)
            {
                ((CheckBox)sender).Checked = false;
                this.lblPerOperation.ForeColor = System.Drawing.Color.Red;
                this.lblPerOperation.Text = "错误号码.";
            }
            else
            {
                if (((CheckBox)sender).Checked)
                {
                    Entity_GMPersonnelInfo gmpSave = new Entity_GMPersonnelInfo();
                    gmpSave.SmsInfoName = strName;
                    gmpSave.SmsInfoMobileNumber = strMobileNo;
                    gmpSave.SmsInfoType = "PERSONAL";
                    gmpSave.SmsInfoDeptName = strDeptName;
                    gmpSave.SmsInfoSelected = true;
                    if (savaToGVIPL(gmpSave))
                    {
                        foreach (Entity_GMPersonnelInfo gmpF in listPersonnelToGV)
                        {
                            if (gmpF.SmsInfoName == strName && gmpF.SmsInfoMobileNumber == strMobileNo && gmpF.SmsInfoType == "PERSONAL")
                            {
                                gmpF.SmsInfoSelected = true;
                            }
                        }
                        this.lblPerOperation.ForeColor = System.Drawing.Color.FromName("#3399FF");
                        this.lblPerOperation.Text = "增加成功.";
                    }
                    else
                    {
                        this.lblPerOperation.ForeColor = System.Drawing.Color.Red;
                        this.lblPerOperation.Text = "手机号重复.";
                        ((CheckBox)sender).Checked = false;
                    }
                }
                else
                {
                    //判断并同步取消GridViewInformPersonnel.
                    Entity_GMPersonnelInfo gmpCencal = new Entity_GMPersonnelInfo();
                    gmpCencal.SmsInfoName = strName;
                    gmpCencal.SmsInfoMobileNumber = strMobileNo;
                    gmpCencal.SmsInfoType = "PERSONAL";
                    gmpCencal.SmsInfoSelected = false;
                    if (cancelToGVIPL(gmpCencal, "PERSONAL"))
                    {
                        foreach (Entity_GMPersonnelInfo gmpF in listPersonnelToGV)
                        {
                            if (gmpF.SmsInfoName == strName && gmpF.SmsInfoMobileNumber == strMobileNo && gmpF.SmsInfoType == "PERSONAL")
                            {
                                gmpF.SmsInfoSelected = false;
                                //gridDataBindByGridViewPersonal();
                            }
                        }
                        this.lblPerOperation.ForeColor = System.Drawing.Color.FromName("#3399FF");
                        this.lblPerOperation.Text = "取消成功.";
                    }
                }
            }
        }
        //3.2、设置分页正常
        protected void GridViewPersonal_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridViewPersonal.DataSource = listPersonnelToGV;
            this.GridViewPersonal.PageIndex = e.NewPageIndex;
            this.GridViewPersonal.DataBind();
        }

        
        /***************************选择联系人操作-系统通讯簿*********************************/
        //一、获取并绑定所有系统通讯簿人员列表：
        //1.1、为GridViewSys绑定所有用户组.
        private bool gridDataBindByGridViewSys()
        {
            try
            {
                bool flag = false;
                if (listSysToGV.Count > 0)
                {
                    gridDataBindByGridViewSys(listSysToGV);
                }
                else
                {
                    listSysUser = bllUser.getAllUser();
                    Entity_GMPersonnelInfo gmPerInfo = null;
                    foreach (Entity_USER_User userInfo in listSysUser)
                    {
                        gmPerInfo = new Entity_GMPersonnelInfo();
                        gmPerInfo.SmsInfoName = userInfo.RealName;
                        gmPerInfo.SmsInfoMobileNumber = userInfo.MobilePhone;
                        gmPerInfo.SmsInfoType = "SYS";
                        //部门赋值在GridViewSys_RowCreated行创建事件中赋值.
                        listSysToGV.Add(gmPerInfo);
                    }
                    if (listSysToGV.Count != 0)
                    {
                        gridDataBindByGridViewSys(listSysToGV);
                    }
                }
                return flag;
            }
            catch (System.Exception ex)
            {
                return false;
                throw ex;
            }
        }
        //1.2、根据传入list为GridViewSys绑定数据.
        private bool gridDataBindByGridViewSys(object objDataSource)
        {
            try
            {
                this.divGVSYS.InnerText = "";
                this.GridViewSys.DataSource = (List<Entity_GMPersonnelInfo>)objDataSource;
                this.GridViewSys.DataBind();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
                throw ex;
            }
        }
        //1.3、行绑定数据时处理：部门赋值.
        protected void GridViewSys_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //根据手机号码取用户部门名称:
                string strMobileNumber = DataBinder.Eval(e.Row.DataItem, "SmsInfoMobileNumber").ToString().Trim();
                Entity_USER_User userParameter = new Entity_USER_User();
                userParameter.MobilePhone = strMobileNumber;
                List<Entity_USER_User> userList = bllUser.getUserListByCondition(userParameter);   //获取一个用户.
                Entity_USER_User userEntity = null;
                List<Entity_USER_Department> deptList = null;
                if (userList.Count == 1)
                {
                    foreach (Entity_USER_User userF in userList)
                    {
                        userEntity = userF;
                    }
                    deptList = bllDept.getDeptListByUser(userEntity);
                    if (deptList.Count != 0)
                    {
                        List<string> deptNameList = new List<string>();
                        foreach (Entity_USER_Department department in deptList)
                        {
                            deptNameList.Add(department.DeptName);
                        }
                        ((DropDownList)e.Row.FindControl("DropDownListSysShowDept")).DataSource = deptNameList;
                        ((DropDownList)e.Row.FindControl("DropDownListSysShowDept")).DataBind();
                    }
                    else
                    {
                        List<string> deptNameList = new List<string>();
                        deptNameList.Add("未设置部门");
                        ((DropDownList)e.Row.FindControl("DropDownListSysShowDept")).DataSource = deptNameList;
                        ((DropDownList)e.Row.FindControl("DropDownListSysShowDept")).DataBind();
                    }
                }
                else
                {
                    //属于错误,有垃圾数据:有重复的手机号码.
                }
            }
        }
        //二、基本功能操作：
        //2.1、实现根据姓名查找功能.
        protected void lBtnSysFind_Click(object sender, EventArgs e)
        {
            this.divGVSYS.InnerText = "";
            string strFindName = this.txtSysFindName.Text.Trim();
            if (strFindName == "")
            {
                gridDataBindByGridViewSys(null);
                this.divGVSYS.InnerText = "请输入名字查找指定用户.";
            }
            else
            {
                Entity_USER_User userParameter = new Entity_USER_User();
                userParameter.RealName = strFindName;
                List<Entity_USER_User> userList = bllUser.getUserListByCondition(userParameter);
                Entity_GMPersonnelInfo gmPerInfo = null;
                List<Entity_GMPersonnelInfo> listGMInfo = new List<Entity_GMPersonnelInfo>();

                if (userList.Count != 0)
                {
                    foreach (Entity_USER_User userInfo in userList)
                    {
                        gmPerInfo = new Entity_GMPersonnelInfo();
                        gmPerInfo.SmsInfoName = userInfo.RealName;
                        gmPerInfo.SmsInfoMobileNumber = userInfo.MobilePhone;
                        //部门赋值在GridViewSys_RowCreated行创建事件中赋值.
                        gmPerInfo.SmsInfoType = "SYS";
                        foreach (Entity_GMPersonnelInfo gmp in listSendPersonnelToGV)
                        {
                            if (gmp.SmsInfoName == gmPerInfo.SmsInfoName && gmp.SmsInfoMobileNumber == gmPerInfo.SmsInfoMobileNumber && gmp.SmsInfoType == "SYS")
                            {
                                gmPerInfo.SmsInfoSelected = gmp.SmsInfoSelected;
                            }
                        }
                        listGMInfo.Add(gmPerInfo);
                    }
                    this.divGVSYS.InnerText = "";
                    gridDataBindByGridViewSys(listGMInfo);
                }
                else
                {
                    gridDataBindByGridViewSys(null);
                    this.divGVSYS.InnerText = "没有用户["+strFindName+"],请检查查询姓名是否输入正确.";
                }
            }
        }
        //2.2、实现查找全部按钮.
        protected void lBtnSysFindALL_Click(object sender, EventArgs e)
        {
            gridDataBindByGridViewSys();
        }
        //2.3、为部门下拉选择赋值.
        protected void ddlSysDeptValuesBind()
        {
            Dictionary<string, decimal> dtyDepts = new Dictionary<string, decimal>();
            List<Entity_USER_Department> deptList = bllDept.getDeptList();
            foreach (Entity_USER_Department dept in deptList)
            {
                dtyDepts.Add(dept.DeptName, dept.DeptID);
            }
            this.ddlSysDeptSelect.DataSource = dtyDepts;
            this.ddlSysDeptSelect.DataTextField = "Key";
            this.ddlSysDeptSelect.DataValueField = "Value";
            this.ddlSysDeptSelect.DataBind();
            this.ddlSysDeptSelect.Items.Insert(0, new ListItem("部门用户查看", "部门用户查看"));
        }
        //2.4、实现选择部门显示部门下人员列表.
        protected void ddlSysDeptSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlSysDeptSelect.SelectedValue == "部门用户查看")
            {
                gridDataBindByGridViewSys();
            }
            else
            {
                decimal dclSelectedDeptID = Convert.ToDecimal(this.ddlSysDeptSelect.SelectedValue);
                List<Entity_USER_User> userListIsDept = bllUser.getUserListByDeptID(dclSelectedDeptID);
                Entity_GMPersonnelInfo gmPerInfo = null;
                List<Entity_GMPersonnelInfo> listGMInfo = new List<Entity_GMPersonnelInfo>();
                foreach (Entity_USER_User userInfo in userListIsDept)
                {
                    gmPerInfo = new Entity_GMPersonnelInfo();
                    gmPerInfo.SmsInfoName = userInfo.RealName;
                    gmPerInfo.SmsInfoMobileNumber = userInfo.MobilePhone;
                    //部门赋值在GridViewSys_RowCreated行创建事件中赋值.
                    listGMInfo.Add(gmPerInfo);
                }
                if (listGMInfo.Count != 0)
                {
                    //this.divGVSYS.InnerText = "";
                    gridDataBindByGridViewSys(listGMInfo);
                }
                else
                {
                    gridDataBindByGridViewSys(null);
                    this.divGVSYS.InnerText = "部门下没有设置用户.";
                }
            }
        }
        //三、操作GridViewInformPersonnel的绑定：
        //3.1、选择或取消时:操作GridViewInformPersonnel的绑定数据并绑定.
        protected void CheckBoxSysUserSelect_CheckedChanged(object sender, EventArgs e)
        {
            GridViewRow gdr = (GridViewRow)((CheckBox)sender).Parent.Parent;

            string strName = "";
            string strMobileNo = "";
            if (gdr.Cells[0].Text.Trim().Equals("&nbsp") || gdr.Cells[0].Text.Trim().Equals(""))
            { 
                strName = ""; 
            }
            else
            { 
                strName = gdr.Cells[0].Text.Trim(); 
            }
            
            if (gdr.Cells[2].Text.Trim().Equals("&nbsp") || gdr.Cells[2].Text.Trim().Equals(""))
            { 
                strMobileNo = ""; 
            }
            else
            { 
                strMobileNo = gdr.Cells[2].Text.Trim(); 
            }
    
            if (strMobileNo.Length!=11)
            {
                ((CheckBox)sender).Checked = false;
                this.lblSysOperation.ForeColor = System.Drawing.Color.Red;
                this.lblSysOperation.Text = "错误号码.";
            }
            else
            {
                if (((CheckBox)sender).Checked)
                {
                    Entity_GMPersonnelInfo gmpSave = new Entity_GMPersonnelInfo();
                    gmpSave.SmsInfoName = strName;
                    gmpSave.SmsInfoMobileNumber = strMobileNo;                    
                    gmpSave.SmsInfoType = "SYS";
                    gmpSave.SmsInfoSelected = true;
                    if (savaToGVIPL(gmpSave))
                    {
                        foreach (Entity_GMPersonnelInfo gmpF in listSysToGV)
                        {
                            if (gmpF.SmsInfoName == strName && gmpF.SmsInfoMobileNumber == strMobileNo && gmpF.SmsInfoType == "SYS")
                            {
                                gmpF.SmsInfoType = "SYS";
                                gmpF.SmsInfoSelected = true;
                            }
                        }
                        this.lblSysOperation.ForeColor = System.Drawing.Color.FromName("#3399FF"); 
                        this.lblSysOperation.Text = "增加成功.";
                    }
                    else
                    {
                        ((CheckBox)sender).Checked = false;
                        this.lblSysOperation.ForeColor = System.Drawing.Color.Red;
                        this.lblSysOperation.Text = "手机号重复.";                        
                    }
                }
                else
                {
                    //判断并同步取消GridViewInformPersonnel.
                    Entity_GMPersonnelInfo gmpCencal = new Entity_GMPersonnelInfo();
                    gmpCencal.SmsInfoName = strName;
                    gmpCencal.SmsInfoMobileNumber = strMobileNo;
                    gmpCencal.SmsInfoType = "SYS";
                    gmpCencal.SmsInfoSelected = false;
                    if (cancelToGVIPL(gmpCencal,"SYS"))
                    {
                        foreach (Entity_GMPersonnelInfo gmpF in listSysToGV)
                        {
                            if (gmpF.SmsInfoName == strName && gmpF.SmsInfoMobileNumber == strMobileNo)
                            {
                                gmpF.SmsInfoType = "SYS";
                                gmpF.SmsInfoSelected = false;                                
                            }
                        }
                        this.lblSysOperation.ForeColor = System.Drawing.Color.FromName("#3399FF"); 
                        this.lblSysOperation.Text = "取消成功.";
                    }
                }
            }
        }
        //3.2、设置分页正常
        protected void GridViewSys_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridViewSys.DataSource = listSysToGV;
            this.GridViewSys.PageIndex = e.NewPageIndex;
            this.GridViewSys.DataBind();    
        }


        /***************************自定义添加联系人*********************************/
        //验证输入手机号是否正确.
        //添加时操作GridViewInformPersonnel的绑定.
        protected void lBtnCustomADD_Click(object sender, EventArgs e)
        {

            string strName = this.txtCustomName.Text.Trim();
            string strMobileNo = this.txtCustomPhoneNumber.Text.Trim();
            //验证是否11位手机号
            if (strMobileNo.Length != 11)
            {
                this.lblCustomAddState.ForeColor = System.Drawing.Color.Red;
                this.lblCustomAddState.Text = "请输入正确的11位手机号码.";
            }
            else
            {
                this.lblCustomAddState.ForeColor = System.Drawing.Color.FromName("#3399FF"); ;
                this.lblCustomAddState.Text = "";
                Entity_GMPersonnelInfo gmp = new Entity_GMPersonnelInfo();
                gmp.SmsInfoName = strName;
                gmp.SmsInfoMobileNumber = strMobileNo;
                gmp.SmsInfoType = "CUSTOM";
                if (savaToGVIPL(gmp))
                {
                    this.txtCustomName.Text = "";
                    this.txtCustomPhoneNumber.Text = "";
                    this.lblCustomAddState.ForeColor = System.Drawing.Color.FromName("#3399FF"); ;
                    this.lblCustomAddState.Text = "增加待发送号码成功.";
                }
                else
                {
                    this.lblCustomAddState.ForeColor = System.Drawing.Color.Red;
                    this.lblCustomAddState.Text = "通知列表中已经存在该手机号码.";
                }
            }
        }


        /***************************短信内容处理*********************************/
        //前台页面javascript处理了:禁止输入空格和回车,统计输入字符数量等.


        /***************************操作通知人员列表*********************************/
        //绑定数据到GridViewInformPersonnel
        private void gridDataBindByGridViewInformPersonnel()
        {
            if (listSendPersonnelToGV.Count > 0)
            {
                this.divGVPersonnel.InnerText = "";
                this.GridViewInformPersonnel.DataSource = listSendPersonnelToGV;
                this.GridViewInformPersonnel.DataBind();                
            }
            if (listSendPersonnelToGV.Count == 0)
            {
                this.divGVPersonnel.InnerText = "点击左侧选择或自定义联系人,将待发送号码信息显示在这里.";
                this.GridViewInformPersonnel.DataSource = null;
                this.GridViewInformPersonnel.DataBind();
            }
        }
        //增加人员并绑定数据到GridViewInformPersonnel:
        private bool savaToGVIPL(Entity_GMPersonnelInfo gmpEntity)
        {
            this.divGVPersonnel.InnerText = "";
            try
            {
                bool flag = true;    //验证手机号码重复.true:不重复;false重复.
                foreach (Entity_GMPersonnelInfo gmpE in listSendPersonnelToGV)
                {
                    if (gmpE.SmsInfoMobileNumber == gmpEntity.SmsInfoMobileNumber)
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag)
                {
                    listSendPersonnelToGV.Add(gmpEntity);
                }

                this.GridViewInformPersonnel.DataSource = listSendPersonnelToGV;
                this.GridViewInformPersonnel.DataBind();
                return flag;
            }
            catch (System.Exception ex)
            {
                return false;
            }

        }
        //取消人员并重新绑定数据到GridViewInformPersonnel:
        private bool cancelToGVIPL(Entity_GMPersonnelInfo gmpEntity, string strType)
        {
            bool flag = false;
            foreach (Entity_GMPersonnelInfo egmp in listSendPersonnelToGV)
            {
                if (egmp.SmsInfoName == gmpEntity.SmsInfoName && egmp.SmsInfoMobileNumber == gmpEntity.SmsInfoMobileNumber && egmp.SmsInfoType == strType)
                {
                    flag = listSendPersonnelToGV.Remove(egmp);
                    if (strType == "PERSONAL")
                    {
                        foreach (Entity_GMPersonnelInfo gmpF in listPersonnelToGV)
                        {
                            if (gmpF.SmsInfoName == gmpEntity.SmsInfoName && gmpF.SmsInfoMobileNumber == gmpEntity.SmsInfoMobileNumber && gmpF.SmsInfoType == "PERSONAL")
                            {
                                gmpF.SmsInfoSelected = false;
                            }
                        }
                        this.GridViewPersonal.DataSource = listPersonnelToGV;
                        this.GridViewPersonal.DataBind();
                    }
                    if (strType == "SYS")
                    {
                        foreach (Entity_GMPersonnelInfo gmpFE in listSysToGV)
                        {
                            if (gmpFE.SmsInfoName == gmpEntity.SmsInfoName && gmpFE.SmsInfoMobileNumber == gmpEntity.SmsInfoMobileNumber)
                            {
                                gmpFE.SmsInfoSelected = false;
                            }
                        }
                        this.GridViewSys.DataSource = listSysToGV;
                        this.GridViewSys.DataBind();
                    }
                    break;
                }
            }
            this.GridViewInformPersonnel.DataSource = listSendPersonnelToGV;
            this.GridViewInformPersonnel.DataBind();
            return flag;
        }
        //GridViewInformPersonnel中的取消操作.
        protected void LinkButtonCancelPersonnel_Click(object sender, EventArgs e)
        {
            GridViewRow gdrow = (GridViewRow)((LinkButton)sender).Parent.Parent;
            //获取当前行的手机号.
            string strMobileNo = "";
            if (gdrow.Cells[1].Text.Trim().Equals("&nbsp") || gdrow.Cells[1].Text.Trim().Equals(""))
            {
                strMobileNo = ""; 
            }
            else
            {
                strMobileNo = gdrow.Cells[1].Text.Trim(); 
            }
            //根据当前行手机号码获取List中的数据.
            foreach (Entity_GMPersonnelInfo gmpE in listSendPersonnelToGV)
            {
                if (gmpE.SmsInfoMobileNumber==strMobileNo)
                {
                    if (gmpE.SmsInfoType == "PERSONAL")
                    {
                        foreach (Entity_GMPersonnelInfo gmpPer in listPersonnelToGV)
                        {
                            if (gmpPer.SmsInfoMobileNumber == strMobileNo && gmpPer.SmsInfoType=="PERSONAL")
                            {
                                gmpPer.SmsInfoSelected = false;
                            }
                        }
                        gridDataBindByGridViewPersonal();
                    }
                    if (gmpE.SmsInfoType == "SYS")
                    {
                        foreach (Entity_GMPersonnelInfo gmpSys in listSysToGV)
                        {
                            if (gmpSys.SmsInfoMobileNumber == strMobileNo && gmpSys.SmsInfoType == "SYS")
                            {
                                gmpSys.SmsInfoSelected = false;
                            }
                        }
                        gridDataBindByGridViewSys();
                    }
                    listSendPersonnelToGV.Remove(gmpE);                                      
                    break;
                }
            }
            this.gridDataBindByGridViewInformPersonnel();
        }


        /***************************短信发送操作*********************************/
        //操作状态显示
        private void operationFeedbackByLabel(string strFeedback)
        {
            this.lblOperationState.Text = "操作状态:";
            this.lblOperationState.Text += strFeedback;
        }
        //取消发送:恢复页面初始值:
        protected void btnCancelSms_Click(object sender, EventArgs e)
        {
            //循环设置list并绑定.
            foreach (Entity_GMPersonnelInfo gmpPer in listPersonnelToGV)
            {
                gmpPer.SmsInfoSelected = false;
            }
            foreach (Entity_GMPersonnelInfo gmpSys in listSysToGV)
            {
                gmpSys.SmsInfoSelected = false;
            }
            listSendPersonnelToGV.Clear();
            Response.Redirect(Request.Url.ToString()); 
        }
        //执行发送:
        protected void btnSendSms_Click(object sender, EventArgs e)
        {
            //System.Drawing.Color.FromName("#3399FF");
            string strContent = "政府办通知:"+ this.txtSmsContent.Text.Trim();
            if (this.txtSmsContent.Text.Trim().Length<3)
            {
                this.lblOperationState.ForeColor = System.Drawing.Color.Red;
                operationFeedbackByLabel("失败:信息内容不少于3个字符.");
            }
            else if (this.txtSmsContent.Text.Trim().Length >60)
            {
                this.lblOperationState.ForeColor = System.Drawing.Color.Red;
                operationFeedbackByLabel("失败: 信息内容超长.");
            }
            else
            {                
                this.lblOperationState.ForeColor = System.Drawing.Color.FromName("#3399FF");
                operationFeedbackByLabel("发送中...");
                List<Entity_COM_Sms_Ready> smsSendList = new List<Entity_COM_Sms_Ready>();
                Entity_COM_Sms_Ready smsReadyEntity = null;
                if (listSendPersonnelToGV.Count == 0)
                {
                    this.lblOperationState.ForeColor = System.Drawing.Color.Red;
                    operationFeedbackByLabel("失败:没有待发送号码.");
                }
                else
                {
                    foreach (Entity_GMPersonnelInfo gmp in listSendPersonnelToGV)
                    {
                        smsReadyEntity = new Entity_COM_Sms_Ready();
                        smsReadyEntity.RdyFromUserID = dclLoginUserID;   //
                        smsReadyEntity.RdyAccessType = "informSms";   //
                        smsReadyEntity.RdyAccessValue = 2003;  //
                        smsReadyEntity.RdyMtnumber = gmp.SmsInfoMobileNumber.Trim();
                        smsReadyEntity.RdyTextContent = strContent.Trim();
                        smsReadyEntity.RdySendFlag = 0;   //未发送
                        smsReadyEntity.RdySendType = 0;  //立即发送
                        smsSendList.Add(smsReadyEntity);
                    }
                    if (smsListReadySend(smsSendList))
                    {
                        this.lblOperationState.ForeColor = System.Drawing.Color.FromName("#3399FF");
                        operationFeedbackByLabel("短信发送操作成功!");
                        this.ckbSendButtonState.Checked = false;
                        this.btnSendSms.Enabled = false;    
                    }
                }
            }
        }
        //短信列表发送方法:
        private bool smsListReadySend(List<Entity_COM_Sms_Ready> srList)
        {
            try
            {
                bool flag = true;
                BLL_SmsOperation bllSms= new BLL_SmsOperation();
                foreach (Entity_COM_Sms_Ready sr in srList)
                {
                    if (!bllSms.addSmsReday(sr))
                    {
                        flag = false;
                    }
                }
                return flag;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }
        //安全锁功能
        protected void ckbSendButtonState_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ckbSendButtonState.Checked)
            {
                this.btnSendSms.Enabled = true;
            }
            else
            {
                this.btnSendSms.Enabled = false;
            }
        }        
    }

    /// <summary>
    /// 用户参数传递的局部实体类.
    /// </summary>
    class Entity_GMPersonnelInfo
    {
        private string smsInfoName = "";                            //收信人姓名
        private bool smsInfoSelected = false;                 //是否选定收信人.
        private string smsInfoMobileNumber = "";           //收信人手机号码.
        private string smsInfoDeptName = "";
        private List<string> smsInfoDeptNameList = null;                 //收信人单位部门名称列表.
        private string smsInfoType = "";                              //确定添加类型:个人通讯簿-"PERSONAL",系统通讯簿增加-"SYS",自定义增加-"CUSTOM".

        public string SmsInfoName
        {
            get { return smsInfoName; }
            set { smsInfoName = value; }
        }

        public string SmsInfoMobileNumber
        {
            get { return smsInfoMobileNumber; }
            set { smsInfoMobileNumber = value; }
        }

        public bool SmsInfoSelected
        {
            get { return smsInfoSelected; }
            set { smsInfoSelected = value; }
        }

        public string SmsInfoDeptName
        {
            get { return smsInfoDeptName; }
            set { smsInfoDeptName = value; }
        }

        public List<string> SmsInfoDeptNameList
        {
            get { return smsInfoDeptNameList; }
            set { smsInfoDeptNameList = value; }
        }

        public string SmsInfoType
        {
            get { return smsInfoType; }
            set { smsInfoType = value; }
        }
    }
}
